import os
import pandas as pd
from pathlib import Path

def process_site_file(input_path, output_dir):
    df = pd.read_csv(input_path)
    
    # 统一列名格式并选择公共列
    required_columns = ['date', 'hour', 'type', '1001A', '1002A', '1003A']
    df = df[[c for c in required_columns if c in df.columns]]
    
    # 转换长格式为宽格式
    pivoted = df.pivot(index=['date', 'hour'], columns='type', values=required_columns[3:])
    pivoted.columns = [f'{col[0]}_{col[1]}' for col in pivoted.columns]
    
    # 创建输出目录
    output_path = Path(output_dir) / Path(input_path).name
    pivoted.reset_index().to_csv(output_path, index=False)

if __name__ == '__main__':
    input_dir = 'data/selected_sites'
    output_dir = 'data/processed_sites'
    os.makedirs(output_dir, exist_ok=True)
    
    for file in Path(input_dir).glob('*.csv'):
        process_site_file(file, output_dir)